<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_Mod extends CI_Model {
    
    const primary_table = 'order';
    const foreign_table = 'order_product';
    
    var $OrderStatus = array();
    var $ci = NULL;

    public function __construct() {        
        parent::__construct();
        $this->ci = & get_instance();
        $this->ci->load->helper('string');
        $this->ci->config->load('webconfig', TRUE);
        $config = $this->ci->config->item('webconfig');
        foreach($config['order_status'] AS $key=>$val) {
            $this->OrderStatus[] = $key;
        }        
    }
    
    //保存订单
    public function save_order($orderNumber, $data = array(), $product = array()) {
        if(is_array($data) && is_array($product)) {
            $data['OrderNumber'] = $orderNumber;
            $this->db->insert(self::primary_table, $data);
            if($this->db->affected_rows() > 0) {
                $insert_id = $this->db->insert_id();
                $sql_insert = array();
                foreach($product AS $list) {
                    $sql_insert[] = "('{$insert_id}', {$list['sid']}, {$list['product_id']}, {$list['price']},{$list['qty']},'{$list['product_name']}')";
                }
                $product_sql = "INSERT INTO `cq6_".self::foreign_table."`(`order_key`,`sid`,`product_id`,`price`,`qty`,`product_name`)values";
                $sql = $product_sql . join($sql_insert, ',');
                $this->db->query($sql);
                if($this->db->affected_rows() > 0) {
                    return TRUE;
                }
            }
        }
        
        return FALSE;
    }
    
    //订单编号生成
    public function order_number($user_id) {
        
        return $user_id .'-'. date('Ymdhis') . '-'. random_string('numeric', 5);
    }
    
    //用订单号或着用户ID获取订单
    public function GetOneOrder($value, $OrderKey = 'OrderNumber') {
        if(in_array($OrderKey, array('OrdID', 'OrderNumber', 'UserID', 'Phone'))) {
            $this->db->select('*')->from(self::primary_table)->where($OrderKey, $value)->order_by('OrdID', 'DESC');
            $OrderRs = $this->db->get();
            return $OrderRs->num_rows() > 0 ? $OrderRs->row_object() : FALSE;
        }
        
        return FALSE;
    }
    
    //按条件查询订单
    public function GetQueryOrder($where, $status = FALSE, $page = FALSE, $select = '*') {
        $this->db->select($select)->from(self::primary_table);
        $this->db->where($where);
        if($status) {
            $this->db->where('order_status', $status);
        }
        $this->db->order_by('OrdID', 'DESC');
        if(is_array($page)) {
            $this->db->limit($page['start'], $page['end']);
        }
        $orderRs = $this->db->get();
        return ($orderRs->num_rows() > 0 ? $orderRs->result_array() : FALSE);
    }
    
    //订单状态更新
    public function updateOrderStatus($OrderNumber, $status) {
        if(in_array($status, $this->OrderStatus)) {
            $data = array('order_status'=> $status);
            $this->db->where('OrderNumber', $OrderNumber);
            $this->db->update(self::primary_table, array('order_status'=> $status));
            return ($this->db->affected_rows() >0 ? TRUE : FALSE);
        }
        
        return FALSE;
    }
    
    //查询指定订单下面产品
    public function GetOrderProduct($number, $whereType = 'OrdID') {
        if($whereType == 'OrdID') {
            $rs = $this->db->get_where(self::foreign_table, array('order_key'=>$number));
            return $rs->num_rows() > 0 ? $rs->result_array() : FALSE;
        }
        
        $query = $this->db->get_where(self::primary_table, array('OrderNumber'=>$number));
        if($query->num_rows() > 0) {
            $order = $query->row_object();
            $rs = $this->db->get_where(self::foreign_table, array('order_key'=>$order->OrdID));
            return $rs->num_rows() > 0 ? $rs->result_array() : FALSE;
        }
        
        return FALSE;
    }
    
}